package com.auto.oa.user.service.impl;

import java.util.List;
import java.util.Map;

import com.auto.oa.menu.domain.OaMenuRoleUserMap;
import com.auto.oa.menu.service.OaMenuServiceFactory;
import com.auto.oa.user.dao.OaUserDAOFactory;
import com.auto.oa.user.domain.OaUserInfo;
import com.auto.oa.user.domain.OaUserInfoExtend;
import com.auto.oa.user.domain.OaUserOrgMap;
import com.auto.oa.user.service.IOaUserInfoService;
import com.system.common.Page;
import com.system.util.BeanUtil;

public class OaUserInfoServiceImpl implements IOaUserInfoService {

	public List<OaUserInfo> queryPageListOaUserInfo(OaUserInfo bean, Page page) throws Exception {
		List<OaUserInfo> resultList = null;
		try {
			Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
			resultList = OaUserDAOFactory.oaUserInfoDAO.queryPageListOaUserInfo(param, page);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

		return resultList;
	}

	public boolean saveOrUpdateOaUserInfo(OaUserInfo oaUserInfo, OaUserOrgMap oaUserOrgMap) throws Exception {
		boolean bool = true;
		try {
			if(BeanUtil.isObjectNotNull(oaUserInfo)){
				if(BeanUtil.isBlank(oaUserInfo.getAuserGuid())){
					//没有设置密码则初始密码与工号一样
					if(BeanUtil.isBlank(oaUserInfo.getAuserPassword())){
						oaUserInfo.setAuserPassword(oaUserInfo.getAuserCode());
					}
					OaUserDAOFactory.oaUserInfoDAO.insertOaUserInfo(oaUserInfo);
					
					//用户组织关系对象 插入用户主键
					oaUserOrgMap.setAuserGuid(oaUserInfo.getAuserGuid());
					OaUserDAOFactory.oaUserOrgMapDAO.insertOaUserOrgMap(oaUserOrgMap);
				}else{
					OaUserDAOFactory.oaUserInfoDAO.updateOaUserInfo(oaUserInfo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return bool;
	}

	public boolean deleteOaUserInfo(String guid) throws Exception {
		boolean bool = true;
		try {
			if(!BeanUtil.isBlank(guid)){
				OaUserDAOFactory.oaUserOrgMapDAO.deleteOaUserOrgMapByUserGuid(guid);
				OaUserDAOFactory.oaUserInfoDAO.deleteOaUserInfo(guid);
			}else{
				bool = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return bool;
	}

	public boolean deleteOaUserInfoByUserCode(String userCode) throws Exception {
		boolean bool = true;
		try {
			if(!BeanUtil.isBlank(userCode)){
				OaUserInfo oaUserInfo = getOaUserInfoByUserCode(userCode);
				if(BeanUtil.isObjectNotNull(oaUserInfo)){
					String auserGuid = oaUserInfo.getAuserGuid();
					
					deleteOaUserInfo(auserGuid);
				}
			}else{
				bool = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return bool;
	}

	public boolean checkUserCodeExists(String userCode) throws Exception {
		boolean bool = true;
		try {
			if(BeanUtil.isObjectNotNull(userCode)){
				OaUserInfo resultUserInfo = OaUserDAOFactory.oaUserInfoDAO.loadOaUserInfoByUserCode(userCode);
				if(BeanUtil.isObjectNull(resultUserInfo)){
					bool = false;
				}
			}else{
				bool = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
		return bool;
	}

	public OaUserInfo getOaUserInfo(String guid) throws Exception {
		OaUserInfo oaUserInfo = null;
		try {
			if(BeanUtil.isObjectNotNull(guid)){
				oaUserInfo = OaUserDAOFactory.oaUserInfoDAO.loadOaUserInfo(guid);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return oaUserInfo;
	}

	public OaUserInfo getOaUserInfoByUserCode(String userCode) throws Exception {
		OaUserInfo oaUserInfo = null;
		try {
			if(BeanUtil.isObjectNotNull(userCode)){
				oaUserInfo = OaUserDAOFactory.oaUserInfoDAO.loadOaUserInfoByUserCode(userCode);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return oaUserInfo;
	}

	public List<OaUserInfoExtend> queryPageListUserOrg(OaUserInfoExtend bean, Page page) throws Exception {
		List<OaUserInfoExtend> resultList = null;
		try {
			Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
			resultList = OaUserDAOFactory.oaUserInfoDAO.queryPageListUserOrg(param, page);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

		return resultList;
	}

	public OaUserInfo getOaUserInfoForLogin(String userCode, String password) throws Exception {
		try {
			OaUserInfo oaUserInfo = null;
			if(BeanUtil.isNotBlank(userCode)){
				OaUserInfo queryUser = new OaUserInfo();
				queryUser.setAuserCode(userCode);
				queryUser.setAuserPassword(password);
				
				Map<String, Object> param = BeanUtil.convertBeanToMap(queryUser);
				
				List<OaUserInfo> resultList = OaUserDAOFactory.oaUserInfoDAO.queryListOaUserInfo(param);
				
				if(!resultList.isEmpty() && resultList.size()==1){
					oaUserInfo = resultList.get(0);
				}
				
			}
			
			return oaUserInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
	}

	public boolean isRoleUser(OaUserInfo oaUserInfo) throws Exception {
		try {
			boolean bool = false;
			
			String auserGuid = oaUserInfo.getAuserGuid();
			OaMenuRoleUserMap oaMenuRoleUserMap = new OaMenuRoleUserMap();
			oaMenuRoleUserMap.setAuserGuid(auserGuid);
			
			List<OaMenuRoleUserMap> oaMenuRoleUserMapList = OaMenuServiceFactory.oaMenuRoleService.queryListOaMenuRoleUserMap(oaMenuRoleUserMap);
			
			if(!oaMenuRoleUserMapList.isEmpty()){
				bool = true;
			}
			
			return bool;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public OaUserInfoExtend getUserOrg(String userCode) throws Exception {
		try {
			OaUserInfoExtend oaUserInfoExtend = new OaUserInfoExtend();
			oaUserInfoExtend.setAuserCode(userCode);
			
			Map<String, Object> param = BeanUtil.convertBeanToMap(oaUserInfoExtend);
			List<OaUserInfoExtend> list = OaUserDAOFactory.oaUserInfoDAO.queryListUserOrg(param);
			
			OaUserInfoExtend result = null;
			
			if(!list.isEmpty()){
				result = list.get(0);
			}
			
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}


}
